public class test08 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        int count;
        int maxCount;
        List<Integer> list;
        TreeNode pre;
        public int[] findMode(TreeNode root) {
            count = 0;
            maxCount = 0;
            pre = null;
            list = new ArrayList<>();
            findMode1(root);
            int[] res = new int[list.size()];
            for(int i = 0;i<res.length;i++){
                res[i] = list.get(i);
            }
            return res;
        }
        private void findMode1(TreeNode root){
            if(root==null){
                return;
            }
            findMode1(root.left);
            if(pre==null || pre.val!=root.val){
                count = 1;
            }else{
                count++;
            }
            if(count>maxCount){
                maxCount = count;
                list.clear();
                list.add(root.val);
            }else if(count==maxCount){
                list.add(root.val);
            }
            pre = root;
            findMode1(root.right);
        }
}
